//
//  MCDateFormatter.m
//  HMmall
//
//  Created by 李李善 on 16/6/27.
//  Copyright © 2016年 吉青. All rights reserved.
//

#import "MCDateFormatter.h"

@implementation MCDateFormatter

+ (MCDateFormatter  *)shareDateFormatter
{
    static MCDateFormatter *_standardUserDefaults = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _standardUserDefaults = [[self alloc]init];
    });
    
    
    return _standardUserDefaults;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _formatter  = [NSDateFormatter new];
        _formatter.dateFormat = @"YYYY/MM/dd HH:mm:ss";
//        self.timeZone = [NSTimeZone localTimeZone];
    }
    return self;
}

+ (NSDateFormatter *)dateFormatter{
    MCDateFormatter *dateFormatter = [MCDateFormatter shareDateFormatter];
    return dateFormatter.formatter;
}

+ (NSString *)currentDateStringWithFormat:(NSString *)format
{
    NSDate *chosenDate = [NSDate date];
    NSDateFormatter *formatter=[self dateFormatter];
    [formatter setDateFormat:format];
    NSString *date = [formatter stringFromDate:chosenDate];
    return date;
}

- (NSDate *)dateFromNumber:(NSNumber *)number
{
    NSTimeInterval timeSeconds = number.doubleValue/1000;
    NSDate *date;
    if( timeSeconds>0){
        date = [NSDate dateWithTimeIntervalSince1970:timeSeconds];
    }else{
        date = [NSDate date];
    }
    return date;
}


- (NSString *)stringFromNumber:(NSNumber *)number
{
    NSTimeInterval timeSeconds = [number floatValue];
    NSString *dateString;
    NSDate *date;
    if( timeSeconds>0){
        date = [NSDate dateWithTimeIntervalSince1970:timeSeconds];
    }else{
        date = [NSDate date];
    }
    dateString = [_formatter stringFromDate:date];
    return dateString;
}


+ (NSString *)publicTimeStringByTimeInterval:(NSNumber *)timeInterval {
    NSTimeInterval timeSeconds = [timeInterval longLongValue];
    NSDate *date1 = [NSDate date];
    
    NSInteger time = ([date1 timeIntervalSince1970] - timeSeconds);
    //秒转分钟
    NSInteger small = time / 60;
    if (small == 0) {
        return [NSString stringWithFormat:@"刚刚"];
    }
    
    if (small < 60) {
        return [NSString stringWithFormat:@"%ld分钟前",small];
    }
    
    // 秒转小时
    NSInteger hours = time/3600;
    if (hours<24) {
        return [NSString stringWithFormat:@"%ld小时前",hours];
    }
    //秒转天数
    NSInteger days = time/3600/24;
    if (days < 30) {
        return [NSString stringWithFormat:@"%ld天前",days];
    }
    //秒转月
    NSInteger months = time/3600/24/30;
    if (months < 12) {
        return [NSString stringWithFormat:@"%ld月前",months];
    }
    //秒转年
    NSInteger years = time/3600/24/30/12;
    return [NSString stringWithFormat:@"%ld年前",years];
}
- (NSString *)dateWithInterval:(NSNumber *)interval Format:(NSString *)format
{NSDate *confromTimesp;
    if( interval>0){
        confromTimesp = [NSDate dateWithTimeIntervalSince1970:[interval longLongValue]];
    }else{
        confromTimesp = [NSDate date];
    }
    
    NSDateFormatter *formatter=self.formatter;
    [formatter setDateFormat:format];
    return [formatter stringFromDate:confromTimesp];
    
}
- (NSString *)dateWithTimer:(NSTimeInterval)timer Format:(NSString *)format
{
    NSDate *confromTimesp;
    if( timer>0){
        confromTimesp = [NSDate dateWithTimeIntervalSince1970:timer];
    }else{
        confromTimesp = [NSDate date];
    }
    
    NSDateFormatter *formatter=self.formatter;
    [formatter setDateFormat:format];
    return [formatter stringFromDate:confromTimesp];
    
}

- (NSTimeInterval)timeIntervalWithTimer:(NSString *)timer Format:(NSString *)format
{
    [self.formatter setDateFormat:format];
    NSDate *date = [self.formatter dateFromString:timer];
    if (!date) {
        date = [NSDate date];
    }
    return [date timeIntervalSince1970];

}

@end




@implementation NSDateFormatter (date)

- (NSString *)stringFromNumber:(NSNumber *)number
{
    NSTimeInterval timeSeconds = [number floatValue];
    NSString *dateString;
    NSDate *date;
    if( timeSeconds>0){
        date = [NSDate dateWithTimeIntervalSince1970:timeSeconds];
    }else{
        date = [NSDate date];
    }
    dateString = [self stringFromDate:date];
    return dateString;
}

@end
